/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hxg.portmgr.core.oper.snmp;

import hxg.portmgr.core.device.server.SwProcess;
import hxg.portmgr.core.oper.snmp.oidgroup.ServerSoftwareRunOidEntry;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;

/**
 *
 * @author hxg
 */
public class ServerSnmpoper extends SnmpOper {

    public ServerSnmpoper(String ip, String readCommunity, String writeCommunity) {
        super(ip, readCommunity, writeCommunity);
    }

    public List<SwProcess> getSoftwareProcess() throws IOException {
        List<SwProcess> list = new ArrayList<>();
        ServerSoftwareRunOidEntry[] entries = ServerSoftwareRunOidEntry.values();
        Map<OID, Map<ServerSoftwareRunOidEntry, Variable>> ifmap = SnmpOperUtils.getVariblesOfOidEntryByRow(entries, SnmpOperUtils.getTarget(ip, readCommunity));
        for (OID ifoid : ifmap.keySet()) {
            Map<ServerSoftwareRunOidEntry, Variable> mp = ifmap.get(ifoid);
            SwProcess process = new SwProcess();
            process.setHrSwIdx(ifoid.toString());
            process.setHrSWRunName(mp.get(ServerSoftwareRunOidEntry.hrSWRunName).toString());
            process.setHrSWRunParameters(mp.get(ServerSoftwareRunOidEntry.hrSWRunParameters).toString());
            process.setHrSWRunPath(mp.get(ServerSoftwareRunOidEntry.hrSWRunPath).toString());
            process.setHrSWRunPerfCPU(mp.get(ServerSoftwareRunOidEntry.hrSWRunPerfCPU).toInt());
            process.setHrSWRunPerfMem(mp.get(ServerSoftwareRunOidEntry.hrSWRunPerfMem).toInt());
            process.setHrSWRunStatus(mp.get(ServerSoftwareRunOidEntry.hrSWRunStatus).toInt());
            process.setHrSWRunType(mp.get(ServerSoftwareRunOidEntry.hrSWRunType).toInt());
            list.add(process);
        }
        return list;
    }

    public void stopSoftwartProcess(SwProcess proc) throws IOException {
        SnmpOperUtils.set(ServerSoftwareRunOidEntry.hrSWRunStatus.getAppendOID(new OID(proc.getHrSwIdx())), 4, SnmpOperUtils.getTarget(ip, writeCommunity));
    }
}
